package cn.com.yang.egg;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;

/**
 * 
 * @author 杨亿金
 * @date 2022年8月6日-上午11:05:58
 * @since 1.0.0
 */
public class LogFile {
	private Writer out;
	public LogFile(File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		this.out = new BufferedWriter(writer);
	}
	/**
	 * 写日志
	 * @param message
	 * @throws IOException
	 */
	public void writeEntry(String message) throws IOException {
		synchronized (out) {
			Date d = new Date();
			out.write(d.toString());
			out.write("\t");
			out.write(message);
			out.write("\r\n");
		}
//		synchronized (this) {
//			Date d = new Date();
//			out.write(d.toString());
//			out.write("\t");
//			out.write(message);
//			out.write("\r\n");
//		}
	}
	/**
	 * synchronized 替代对象的this关键字
	 * @param message
	 * @throws IOException
	 */
	public synchronized void writeEntry2(String message) throws IOException {
		Date d = new Date();
		out.write(d.toString());
		out.write("\t");
		out.write(message);
		out.write("\r\n");
//		synchronized (this) {
//			Date d = new Date();
//			out.write(d.toString());
//			out.write("\t");
//			out.write(message);
//			out.write("\r\n");
//		}
	}
	
	public void close() throws IOException {
		out.flush();
		out.close();
	}
}
